
// T3_EditorDoc.cpp : implementation of the CT3_EditorDoc class
//

#include "stdafx.h"
#include "T3_Editor.h"

#include "T3_EditorDoc.h"
#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CT3_EditorDoc

IMPLEMENT_DYNCREATE(CT3_EditorDoc, CDocument)

BEGIN_MESSAGE_MAP(CT3_EditorDoc, CDocument)
	ON_COMMAND(ID_FILE_RUN_GAME_WITH_MAP, &CT3_EditorDoc::OnFileRunGameWithMap)
END_MESSAGE_MAP()


// CT3_EditorDoc construction/destruction

CT3_EditorDoc::CT3_EditorDoc()
: m_GameStage(NULL)
{
	// TODO: add one-time construction code here

}

CT3_EditorDoc::~CT3_EditorDoc()
{
	UnloadGameStage();
}

BOOL CT3_EditorDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	UnloadGameStage();
	m_GameStage = new GameStage();

	return TRUE;
}




// CT3_EditorDoc serialization

void CT3_EditorDoc::Serialize(CArchive& ar)
{
	char filename[1024];
	CStringToChar( filename, ar.m_strFileName );

	if (ar.IsStoring())
	{
		// TODO: add storing code here

		// FIXME: Can't save stage to .lua while file is opened as CArchive
		//if (m_GameStage)
		//{
		//	m_GameStage->SaveToFile( filename );
		//}
	}
	else
	{
		// TODO: add loading code here
		UnloadGameStage();
		m_GameStage = new GameStage();

		m_GameStage->LoadFromFile( filename );

		// Update tile toolbox
		CTileWnd* tileWnd = ((CMainFrame*)AfxGetMainWnd())->GetTileWnd();
		tileWnd->UpdateTileList();
	}
}


BOOL CT3_EditorDoc::OnSaveDocument( LPCTSTR lpszPathName )
{
	ENSURE(lpszPathName);

	CFileException fe;
	CFile* pFile = NULL;
	pFile = GetFile(lpszPathName, CFile::modeCreate |
		CFile::modeReadWrite | CFile::shareExclusive, &fe);

	if (pFile == NULL)
	{
		ReportSaveLoadException(lpszPathName, &fe,
			TRUE, AFX_IDP_INVALID_FILENAME);
		return FALSE;
	}

	// Use GameStage::SaveToFile instead of MFC serialization
	char filename[1024];
	CStringToChar( filename, lpszPathName );

	if (m_GameStage)
	{
		m_GameStage->SaveToFile( filename );
	}

	SetModifiedFlag(FALSE);     // back to unmodified

	return TRUE;        // success
}

void CT3_EditorDoc::UnloadGameStage()
{
	if ( !m_GameStage ) return;

	AREA_TRIGGER* trigger;
	while ( trigger = GetFirstAreaTrigger() )
	{
		RemoveAreaTriggerFromGame( trigger );
	}

	TextureManager::Instance().Reset();

	GW_SAFE_DELETE( m_GameStage );
}

// CT3_EditorDoc diagnostics

#ifdef _DEBUG
void CT3_EditorDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CT3_EditorDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}

#endif //_DEBUG


// CT3_EditorDoc commands

void CT3_EditorDoc::OnFileRunGameWithMap()
{
	// run game with current map


	if ( !this->m_strPathName.GetLength() )
		return;

#if defined _DEBUG
	char gameAppName[256] = ".\\Debug\\Platform2DGameTest.exe";
#else
	char gameAppName[256] = ".\\Platform2DGameTest.exe";
#endif

	char param[256] = { 0 };
	char path[1024] = { 0 };
	CStringToChar( path, this->m_strPathName );
	sprintf( param, "-map %s", path );

	ShellExecuteA( NULL, "open", gameAppName, param, NULL, SW_SHOW );
}
